package com.yy.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.yy.common.constant.PmsConstants;
import com.yy.dto.OaRepairDTO;
import com.yy.system.service.ISysConfigService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yy.common.annotation.Log;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.enums.BusinessType;
import com.yy.domain.OaRepair;
import com.yy.service.IOaRepairService;
import com.yy.common.utils.poi.ExcelUtil;
import com.yy.common.core.page.TableDataInfo;

/**
 * 维修工单Controller
 * 
 * @author yy
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/oa/repair")
public class OaRepairController extends BaseController
{
    @Autowired
    private IOaRepairService oaRepairService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询维修工单列表
     */
    @PreAuthorize("@ss.hasPermi('oa:repair:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaRepair oaRepair)
    {
        startPage();
        List<OaRepair> list = oaRepairService.selectOaRepairList(oaRepair);
        return getDataTable(list);
    }
    /**
     * 获取维修工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('oa:repair:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(oaRepairService.selectOaRepairById(id));
    }

    /**
     * 新增维修工单
     */
    @PreAuthorize("@ss.hasPermi('oa:repair:add')")
    @Log(title = "维修工单", businessType = BusinessType.INSERT)
    @PostMapping("addRepair")
    public AjaxResult addRepair(@RequestBody OaRepair oaRepair)
    {
        //报修时间
        oaRepair.setApplyTime(new Date());
        //处理维修编号
        String key = configService.selectConfigByKey(PmsConstants.OA_PREFIX_WX );
        oaRepair.setNo(key+ IdUtil.getSnowflakeNextIdStr());
        //生成title
        if(StrUtil.isBlank(oaRepair.getApplyHouse())){
            //说明不是业主报修改
            oaRepair.setTitle("【"+oaRepair.getApplyName()+"】发起的报修工单");
        }else{
            //说明是业主报修改
            oaRepair.setTitle("商铺【"+oaRepair.getApplyHouse()+"】发起的报修工单");
        }
        //状态为待受理
        oaRepair.setRepairState(PmsConstants.OA_REPAIR_STATE_APPLY);
        //回访状态
        oaRepair.setReturnState(PmsConstants.OA_RETURN_STATE_UN_VISIT);
        //创建人和创建时间
        oaRepair.setCreateBy(getUsername());
        oaRepair.setCreateTime(new Date());
        return toAjax(oaRepairService.insertOaRepair(oaRepair));
    }

    /**
     * 受理工单
     */
    @PostMapping("applyRepair")

    public AjaxResult applyRepair(@RequestBody OaRepairDTO repairDTO){
        return toAjax(this.oaRepairService.applyRepair(repairDTO));
    }


    /**
     * 现场确认
     */
    @PostMapping("confirmedRepair")

    public AjaxResult confirmedRepair(@RequestBody OaRepairDTO repairDTO){
        return toAjax(this.oaRepairService.confirmedRepair(repairDTO));
    }

    /**
     * 维修确认
     */
    @PostMapping("doRepairConfirm")

    public AjaxResult doRepairConfirm(@RequestBody OaRepairDTO repairDTO){
        return toAjax(this.oaRepairService.doRepairConfirm(repairDTO));
    }

    /**
     * 维修评价
     */
    @PostMapping("doRepairScore")

    public AjaxResult doRepairScore(@RequestBody OaRepairDTO repairDTO){
        return toAjax(this.oaRepairService.doRepairScore(repairDTO));
    }

    /**
     * 物业回访
     */
    @PostMapping("doRepairVisit")

    public AjaxResult doRepairVisit(@RequestBody OaRepairDTO repairDTO){
        return toAjax(this.oaRepairService.doRepairVisit(repairDTO));
    }

    /**
     * 撤销
     */
    @PostMapping("doCancel/{id}")

    public AjaxResult doCancel(@PathVariable Long id){
        return toAjax(this.oaRepairService.doCancel(id));
    }

    /**
     * 导出维修工单列表
     */
    @PreAuthorize("@ss.hasPermi('oa:repair:export')")
    @Log(title = "维修工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaRepair oaRepair)
    {
        List<OaRepair> list = oaRepairService.selectOaRepairList(oaRepair);
        ExcelUtil<OaRepair> util = new ExcelUtil<OaRepair>(OaRepair.class);
        util.exportExcel(response, list, "维修工单数据");
    }
}
